package com.shopping_back.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.List;
import java.util.Map;

/**
 * 仪表盘数据 MyBatis Mapper
 */
@Mapper
public interface DashboardMapper extends BaseMapper<Object> {

    /**
     * 查询订单状态分布
     */
    @Select("""
        SELECT status, COUNT(*) AS count
        FROM order_main
        GROUP BY status
    """)
    List<Map<String, Object>> selectOrderStatusDistribution();

    /**
     * 查询商品销量排行榜 (Top N)
     * 修复 only_full_group_by 模式下的 SQL 错误：添加 sku_name 到 GROUP BY
     */
    @Select("""
        SELECT oi.sku_name AS skuName, SUM(oi.quantity) AS sales
        FROM order_item oi
        GROUP BY oi.sku_id, oi.sku_name
        ORDER BY sales DESC
        LIMIT #{topN}
    """)
    List<Map<String, Object>> selectTopSellingSkus(@Param("topN") int topN);

    /**
     * 查询促销活动参与度对比
     */
    @Select("""
        SELECT pa.name AS activityName, COALESCE(SUM(oi.quantity), 0) AS quantity
        FROM promotion_activity pa
        LEFT JOIN order_item oi ON pa.id = oi.activity_id
        GROUP BY pa.id, pa.name
    """)
    List<Map<String, Object>> selectPromotionParticipation();

    /**
     * 查询评价星级分布
     */
    @Select("""
        SELECT score, COUNT(*) AS count
        FROM evaluation
        GROUP BY score
    """)
    List<Map<String, Object>> selectEvaluationRatingDistribution();
}